﻿<?php
//apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('allow_url_fopen', 'On');
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
set_time_limit(1000);

function getURLDestino($URLEncurtada) {
		
	$arrHeader = get_headers($URLEncurtada);	
	$arrLocation = array();
	$arrServidores = array(
						'www.wupload.com', 'www.filesonic.com', 'upmirror.com', 'uploaded.to', 'www.megaupload.com', 'www.fileserve.com', 'www.uploadstation.com', 'bitshare.com', 'uploaded.com'
						);
	$strServidores = implode('|', $arrServidores);
	
	foreach($arrHeader as $strValue) {
		if(strstr($strValue, 'Location:'))
			$arrLocation[] = $strValue;
	}
		
	$intPosicoes = count($arrLocation);
		
	if($intPosicoes > 1) {
		$strURLDestino = $arrLocation[$intPosicoes - 1];
	} else {
		$strURLDestino = $arrLocation[0];
	}		$strERLocation = '#(http://('.$strServidores.')/[^ ]{1,100})#';
	
	preg_match_all($strERLocation, $strURLDestino, $arrURLDestinoFinal);	
	$strURLDestino = $arrURLDestinoFinal[0][0];	
	return $strURLDestino;
}

function findURLDowload($strURL) {

	$arrLinks = array(
						'www.sharingmatrix.com', 'ul.to', 'www.wupload.com', 'www.filesonic.com', 'goo.gl', 'lnk.co', 'bit.ly',
						'upmirror.com', 'uploaded.to', 'www.megaupload.com', 'www.fileserve.com', 'www.uploadstation.com',
						'bitshare.com', 'w11.easy-share.com', 'lix.in', 'uploaded.com'
					);
	
	echo '<span class="text-title">Loading page...';
	ob_flush();
	flush();
			
	if(!$strCodigoFonte = @file_get_contents($strURL)) {
		echo ' FAILED!<br> - Invalid URL';
		return false;
	} else {
	echo ' COMPLETE!<br><br>Analyzing source code...';
	ob_flush();
	flush();
	}
	
	$strLinks = implode('|', $arrLinks);
	$strER1 = '#(http://('.$strLinks.')/[^"<> ]{1,100})#';
	
	preg_match_all($strER1, $strCodigoFonte, $arrMatches);
	
	$intLinksEncontrados = count($arrMatches[0]);
	
	if($intLinksEncontrados == 0) {
		echo ' COMPLETE!</span><br><span class="text-simple"> - Sorry, no valid link found</span>';
		return;
	}
	
	echo ' COMPLETE!</span><br><span class="text-simple"> - <span class="text-destak">'.$intLinksEncontrados.'</span>  Link(s) found. (All Servers)</span>';
	ob_flush();
	flush();
	
	return $arrMatches[0];
	
}

function listURLDownload($arrURLs, $arrServidores, $intIndice) {

	$arrEncurtadores = array('bit.ly','lnk.co','goo.gl', 'ul.to');
	$intCountLinks = 1;
	
	if(empty($arrServidores))
		$arrServidores[] = 'Todos';
	
	foreach($arrURLs as $strValue) {
	
		if($intCountLinks >= $intIndice || $intIndice == 'todos') {
			foreach ($arrEncurtadores as $strEncurtador) {
				if(strstr($strValue, $strEncurtador))
					$strValue = getURLDestino($strValue);
			}
		
			foreach ($arrServidores as $strServidor) {
				
				if(strstr($strValue, $strServidor) || $strServidor == 'Todos') {
					$strNomeArquivo = findNomeArquivo($strValue);
					
					echo '
						<span class="text-title">'.$intCountLinks.' - </span><span class="text-file-name">'.$strNomeArquivo.'</span><br>
						<a href="'.$strValue.'" target="_blank">'.$strValue.'</a><br><br>';
					ob_flush();
					flush();
					
				}
				
			}
		}
		$intCountLinks++;
	}
	echo '<span class="text-title">FINISH!</span>';
}

function findNomeArquivo($strUrl) {
	
	$arrServidores = array('www.sharingmatrix.com/file/', 'www.wupload.com/file/', 'megaupload.com/files/', 'www.fileserve.com/file/', 'filesonic.com/file/');
	$strServidores = implode('|', $arrServidores);
	$strCodigoFonte = @file_get_contents($strUrl);
	
	$strER1 = '#('.$strServidores.')[^"]{1,100}#';	
	preg_match_all($strER1, $strCodigoFonte, $arrMatches);
	if(isset($arrMatches[0][0])) {
		$arrPedacos = explode('/', $arrMatches[0][0]);
		$intTamanho = count($arrPedacos);
		return $arrPedacos[$intTamanho - 1];
	} else
		return '<strong>Sorry, the file name not found or link unavailable</strong>';
}
	
?>